package design.principles.liskov$substitution.before;

/**
 * @author AtoVio, Winter Yuan
 */
public class Test {
    public static void main(String[] args) {
        Rectangle r1 = new Rectangle(20, 10);
        resize(r1);
        printRectangle(r1);

        System.out.println("======================");

        Square s1 = new Square();
        s1.setWidth(20);
        resize(s1); // 此处进入死循环。
        printRectangle(s1);
        /*
            基类Rectangle出现的地方，用子类Square替换，出现了死循环。说明这个设计违反了里氏替换原则。需要改进
         */
    }

    // 扩宽
    public static void resize(Rectangle rectangle) {
        while (rectangle.getWidth() <= rectangle.getLength()){
            rectangle.setWidth(rectangle.getWidth() + 1);
        }
    }

    public static void printRectangle(Rectangle rectangle) {
        System.out.printf("长为：%4.2f\n", rectangle.getLength());
        System.out.printf("宽为：%4.2f\n", rectangle.getWidth());
    }
}
